package com.zenitera.bigdata.tableapiflinksql;

import com.zenitera.bigdata.bean.WaterSensor;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.Over;
import org.apache.flink.table.api.Table;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;

import java.time.Duration;

import static org.apache.flink.table.api.Expressions.*;

/**
 * Table API - Over Windows - Bounded Over Windows
 * 向前推一个时间单位得到窗口  lit(2).second()
 */
public class Flink05_TableApi_OverWindow02 {
    public static void main(String[] args) {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);
        SingleOutputStreamOperator<WaterSensor> waterSensorStreamSource = env
                .fromElements(
                        new WaterSensor("sensor_1", 1000L, 10),
                        new WaterSensor("sensor_1", 4000L, 40),
                        new WaterSensor("sensor_1", 2000L, 20),
                        new WaterSensor("sensor_2", 3000L, 30),
                        new WaterSensor("sensor_1", 5000L, 50),
                        new WaterSensor("sensor_2", 6000L, 60))
                .assignTimestampsAndWatermarks(
                        WatermarkStrategy
                                .<WaterSensor>forBoundedOutOfOrderness(Duration.ofSeconds(1))
                                .withTimestampAssigner((element, recordTimestamp) -> element.getTs())
                );

        StreamTableEnvironment tableEnv = StreamTableEnvironment.create(env);
        Table table = tableEnv.fromDataStream(waterSensorStreamSource, $("id"), $("ts").rowtime(), $("vc"));
        table
                .window(Over.partitionBy($("id")).orderBy($("ts")).preceding(lit(3).second()).as("w"))
                .select($("id"), $("ts"), $("vc").sum().over($("w")).as("vc_sum"))
                .execute()
                .print();

        try {
            env.execute();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}

/*
+----+--------------------------------+-------------------------+-------------+
| op |                             id |                      ts |      vc_sum |
+----+--------------------------------+-------------------------+-------------+
| +I |                       sensor_1 | 1970-01-01 00:00:01.000 |          10 |
| +I |                       sensor_1 | 1970-01-01 00:00:02.000 |          30 |
| +I |                       sensor_2 | 1970-01-01 00:00:03.000 |          30 |
| +I |                       sensor_1 | 1970-01-01 00:00:04.000 |          70 |
| +I |                       sensor_1 | 1970-01-01 00:00:05.000 |         110 |
| +I |                       sensor_2 | 1970-01-01 00:00:06.000 |          90 |
+----+--------------------------------+-------------------------+-------------+
 */